var _Global_Data = {

    keyword_lists : [],          //关键字列表

    MESSAGE_TYPE_TEXT : {
        1 : '纯文本消息',
        2 : '图片',
        3 : '音频',
        4 : '视频',
        5 : '音乐',
        6 : '图文消息'
    },                           //关键字回复类型映射表

    current_reply : {},         //当前查看的关键字回复详情

    keyword : '',               //关键字

    reply_type : '',            //回复类型

    reply_text : '',            //回复内容

    news_contents : [
        {
            title : '',
            description : '',
            article_url : '',
            cover_url : ''
        }
    ]                           //图文消息

};

var KeywordResource = new Resource( API_LIST.KEYWORD_REPLY , Vue.http );

!function reviseAisle() {

    var vm = new Vue({

        el : "#dashboard",

        data : _Global_Data,

        created : function(){

            this.initKeywordList();

        },

        computed : {

        },

        methods : {
            /**
             * 选择类型
             * @param e 事件events
             */
            selectType : function selectType( e ){

                this.reply_type = e.target.value;

            },
            /**
             * 选择类型
             * @param e 事件events
             */
            modalSelectType : function modalSelectType( e ){

                this.current_reply.message_type = e.target.value;

                if( parseInt(this.current_reply.message_type) === 6 && !this.current_reply.data ) {

                    this.current_reply.data = {

                        "url": "",
                        "image": "",
                        "title": "",
                        "description": ""

                    }

                }

            },
            /**
             * input失去焦点时校验是否为合法网址格式
             * @return {undefined}
             */
            blurEvent : function blurEvent( param , type ) {

                var reg = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;

                switch (type) {

                    case 'new':

                        if( this.news_contents[0][param] && !reg.test(this.news_contents[0][param]) ) {

                            alert('不合法的网址格式！');

                            this.news_contents[0][param] = '';

                        }

                        break;
                    case 'edit':

                        if( this.current_reply.data[param] && !reg.test(this.current_reply.data[param]) ) {

                            alert('不合法的网址格式！');

                            this.current_reply.data[param] = '';

                        }

                        break;

                }



            },
            /**
             * 保存关键字
             * @return {undefined}
             */
            keywordSave : function confirmPost(){

                var params = {};

                var judge_status = true;

                switch (parseInt(this.reply_type)) {

                    case 1:

                        params = {

                            message_type : this.reply_type,

                            keyword : this.keyword,

                            reply : this.reply_text

                        };

                        if( this.keyword === '' || this.reply_text === '' ) {

                            judge_status = true;

                        }else {

                            judge_status = false;

                        }

                        break;

                    case 6:

                        params = {

                            message_type : this.reply_type,

                            keyword : this.keyword,

                            article_url : this.news_contents[0].article_url,

                            cover_url : this.news_contents[0].cover_url,

                            title : this.news_contents[0].title,

                            description : this.news_contents[0].description

                        };

                        if( this.keyword === '' || this.news_contents[0].article_url === ''
                            || this.news_contents[0].cover_url === '' || this.news_contents[0].title === ''
                            || this.news_contents[0].description === '' ) {

                            judge_status = true;

                        }else {

                            judge_status = false;

                        }

                        break;
                }

                if( judge_status ){

                    alert('存在未填信息');

                    return;

                }

                this.dataPost( params );

            },

            /**
             * 保存数据的post
             * @return {undefined}
             */
            dataPost : function dataPost( params ){

                var self = this;

                App.blockUI('#add_keyword');

                KeywordResource.store( params )
                            .then(function(res){

                                if( parseInt(res.data.code) !== 0 )throw new Error(res.data.error);

                                alert('提交成功！');

                                self.initKeywordList();

                                self.dataReset();

                            })
                            .catch(function(err){

                                console.log(err.message);

                                alert(err.message);

                            })
                            .then(function(){App.unblockUI('#add_keyword');})

            },

            /**
             * 查看关键字详情
             * @param id
             */
            detailRevise : function detailWatch( id ){

                this.current_reply = this.keyword_lists.filter(function(keyword){return keyword.id==id})[0];

                $('.modal-type-selector').val(this.current_reply.message_type).trigger('change');

            },

            /**
             * 更新关键字信息
             * @param id
             */
            detailUpdate : function detailUpdate( id ){

                var params = {};

                var judge_status = true;

                switch (parseInt(this.current_reply.message_type)) {

                    case 1:

                        params = {

                            message_type : this.current_reply.message_type,

                            keyword : this.current_reply.keyword,

                            reply : this.current_reply.reply

                        };

                        if( this.current_reply.reply === '' || this.current_reply.message_type === '') {

                            judge_status = true;

                        }else {

                            judge_status = false;

                        }

                        break;

                    case 6:

                        params = {

                            message_type : this.reply_type,

                            keyword : this.keyword,

                            article_url : this.current_reply.data.article_url,

                            cover_url : this.current_reply.data.cover_url,

                            title : this.current_reply.data.title,

                            description : this.current_reply.data.description

                        };

                        if( this.current_reply.keyword === '' || this.current_reply.data.article_url === ''
                            || this.current_reply.data.cover_url === '' || this.current_reply.data.title === ''
                            || this.current_reply.data.description === '' ) {

                            judge_status = true;

                        }else {

                            judge_status = false;

                        }

                        break;
                }

                if( judge_status ){

                    alert('存在未填信息');

                    return;

                }

                App.blockUI('#detail_form');

                KeywordResource.update( id , params )
                            .then(function (res) {

                                if (parseInt(res.data.code) !== 0)throw new Error(res.data.error);

                                alert('更新成功！');

                                $('.close').trigger('click');

                            })
                            .catch(function (err) {

                                console.log(err);

                                alert(err.message);

                            })
                            .then(function(){App.unblockUI('#detail_form')})
            },

            /**
             * 关键字删除
             * @param id    需要删除的keyword的id
             */
            keywordDelete : function keywordDelete(id){

                var self = this;

                var confirm_delete = confirm('确认要删除此关键字吗？');

                if( confirm_delete ){

                    App.blockUI('#keyword_list');

                    KeywordResource.destroy(id)
                        .then(function (res) {

                            if (parseInt(res.data.code) !== 0)throw new Error(res.data.error);

                            self.keyword_lists.forEach(function(keyword,index){

                                if( keyword.id == id ){

                                    self.keyword_lists.splice(index,1);

                                }

                            });

                            alert('删除成功！');

                        })
                        .catch(function (err) {

                            console.log(err);

                            alert(err.message);

                        })
                        .then(function(){App.unblockUI('#keyword_list')})

                }else {

                    return;

                }

            },
            /**
             * 初始化数据
             * @return {promise}
             */
            initKeywordList : function initData(){

                var self = this;

                App.blockUI('#personal_menu');

                return KeywordResource.index()
                                .then(function(res){

                                    if( parseInt(res.data.code) !== 0 )throw new Error(res.data.error);

                                    self.keyword_lists = res.data.replies;

                                })
                                .catch(function(err){

                                    console.log(err.message);

                                    alert( err.message );

                                })
                                .then(function(){App.unblockUI('#personal_menu');})

            },
            /**
             * 重置数据
             * @return {undefined}
             */
            dataReset : function dataReset(){

                $('.type-selector').val('').trigger('change');

                $(" button[type='reset'] ").trigger('click');

            }
        }
    })

}();